// pages/tools/allServices/allServices.js
const app = getApp();
const util = require("../../../utils/util");
var sIndex = "";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showtk: false,
        showRes: false,
        allData: [{
            name: "政务办事",
            info: []
        }, {
            name: "教育办事",
            info: []
        }, {
            name: "交通出行",
            info: []
        }, {
            name: "车辆服务",
            info: []
        }, {
            name: "民生服务",
            info: []
        }],
        allservList: [],
        menuIndex: 0,
        count: 0,
        tabHeight: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(opt) {
        //获取胶囊位置信息
        let menuButtonObject = wx.getMenuButtonBoundingClientRect();
        this.setData({
            navtop: menuButtonObject.top,
            inputHeight: menuButtonObject.height,
        })
        sIndex = opt.menuIndex ? opt.menuIndex : 0;

        // 获取顶部的距离
        var query = wx.createSelectorQuery()
        query.select('.navbar').boundingClientRect((res) => {
            this.setData({
                topHeight: res.height
            })
        }).exec()

        // 获取顶部搜索的距离
        var query = wx.createSelectorQuery()
        query.select('.search').boundingClientRect((res) => {
            this.setData({
                searchHeight: res.height
            })
        }).exec()

        // 获取屏幕高度
        wx.getSystemInfo({
            success: (res) => {
                var clientHeight = res.windowHeight,
                    clientWidth = res.windowWidth,
                    rpxR = 750 / clientWidth;
                var calc = clientHeight * rpxR;
                this.setData({
                    winHeight: calc,
                    clientHeight,
                });
            }
        });
        util.getUrl(app.globalData.url).then((res) => {
            this.data.allData.forEach((v, k) => {
                this.getallBsy(v.name, k)
            })
        })

    },

    menuClick(e) {
        let menuIndex = e.currentTarget.dataset.index;
        this.setData({
            menuIndex,
            toView1: 'L' + menuIndex
        })
    },

    // 全部办事
    getallBsy(type, index) {
        let toolData = {
            action: "tools",
            city: wx.getStorageSync("city"),
            keywords: "",
            version: app.globalData.version,
            type,
        };
        util.request('service.php', toolData, ).then(res => {
            let resData = res.data.data;
            this.data.count++;
            this.data.allData[index].info = resData;
            this.setData({
                allData: this.data.allData
            })

            if (this.data.count == this.data.allData.length) {
                this.setData({
                    allData: this.data.allData
                })
                this.data.allData.forEach((v, k) => {

                    var query = wx.createSelectorQuery()
                    setTimeout(() => {
                        query.select('#L' + k + '.chaxun-box').boundingClientRect((res) => {
                            if (res) {
                                this.data.tabHeight.push(res.height)
                                this.setData({
                                    tabHeight: this.data.tabHeight,
                                })
                            }

                        }).exec()
                    }, 200);
                    if (v.info && v.info.length >= 1) {
                        v.info.forEach(item => {
                            this.data.allservList.push(item)
                            this.setData({
                                allservList: this.data.allservList,
                            })
                        })
                    }

                })

                this.setData({
                    menuIndex: sIndex,
                })

                this.setData({
                    toView: 'L' + this.data.menuIndex,
                })

                this.setData({
                    toView1: this.data.toView,
                })


            }
        })
    },
    scroll: function(e) {
        let scrollViewTop = e.detail.scrollTop;
        let tabHeight = this.data.tabHeight;
        var scroArr = [];
        var currNum = 0;
        tabHeight.forEach((item, index) => {
            if (index == 0) {
                scroArr.push(tabHeight[index]);
            } else {
                if (currNum) {
                    currNum = tabHeight[index] + currNum;
                } else {
                    currNum = tabHeight[index] + tabHeight[index - 1];
                }
                scroArr.push(currNum);
            }
        })

        let lastIndex = tabHeight.length - 1;
        for (let i = 0; i < scroArr.length; i++) {
            if (scrollViewTop >= 0 && scrollViewTop < scroArr[i]) {
                this.setData({
                    menuIndex: 0,
                    toView: 'L' + 0,
                })
                return;
            } else if (scrollViewTop >= scroArr[i] && scrollViewTop < scroArr[i + 1]) {
                this.setData({
                    menuIndex: i + 1,
                    toView: 'L' + (i + 1),
                })
                return;
            } else if (scrollViewTop >= scroArr[scroArr.length - 1]) {
                this.setData({
                    menuIndex: scroArr.length - 1,
                    toView: 'L' + (scroArr.length - 1),
                })
                return;
            }
        }
    },

    // 服务点击
    navDetail(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid;
        let title = e.currentTarget.dataset.title;
        if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1) {
            this.setData({
                showtk: true
            })
            util.navContact(linkname, path)
        } else {
            let scene = util.getScene(wx.getLaunchOptionsSync().scene);
            util.navMethod(path, appid, title, scene);
        }
    },

    // 搜索
    inputConfirm(e) {

    },
    inputTap(e) {
        let keywords = e.detail.value;
        let serachRes = [];
        serachRes = this.data.allservList.filter(v => {
            if (v.title.includes(keywords)) {
                return v;
            }
        })
        this.setData({
                keywords,
                serachRes
            })
            // this.serachRes.length >= 1
        if (this.data.keywords != '') {
            this.setData({
                showRes: true,
            })
        } else {
            this.setData({
                showRes: false,
            })
        }

        if (this.data.keywords === '') {
            this.setData({
                serachRes: [],
                showRes: false,
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})